package Alarm.begin;


import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("unused")
public class AlarmActivity extends Activity {
	
	private PendingIntent pendingIntent;
    /** Called when the activity is first created. */
	
	
	int PICK_CONTACT;
	//ContentResolver cr = getContentResolver();
	
	//String contactId,name,number = null ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final TextView Pnumber = new TextView(this);
        
        Button goalarm = (Button) findViewById(R.id.goalarm);
       // final TimePicker timePickeralarm = (TimePicker) findViewById(R.id.timePickeralarm);
        //DatePicker datepickeralarm;
        //datepickeralarm.init(year, monthOfYear, dayOfMonth, onDateChangedListener)
       
       
    
        goalarm.setOnClickListener(new Button.OnClickListener(){

        	  @Override
        	  public void onClick(View arg0) {
        	   // TODO Auto-generated method stub
        	  //datepickeralarm.
        		//  int h=timePickeralarm.getCurrentHour();
        	     //   int m=timePickeralarm.getCurrentMinute();
        	        
        	        
        	       // Toast.makeText(this, , Toast.LENGTH_LONG).show();
        	        
        	        int k = 5;
        	        
        	   Intent myIntent = new Intent(AlarmActivity.this, MyAlarmService.class);
        	   pendingIntent = PendingIntent.getService(AlarmActivity.this, 0, myIntent, 0);

        	            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        	            Calendar calendar = Calendar.getInstance();
        	            calendar.setTimeInMillis(System.currentTimeMillis());
        	            calendar.add(Calendar.SECOND, k);
        	            //calendar.add(Calendar., value)
        	            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        	         
        	   Toast.makeText(AlarmActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
        	  }});
    
        
        
        Button unreg = (Button) findViewById(R.id.unreg);
        
        unreg.setOnClickListener(new Button.OnClickListener(){



        	 @Override

        	 public void onClick(View arg0) {

        	  // TODO Auto-generated method stub
        		 System.out.println("here2------------------------------hfjhfj----------->>>>>>>");
        	  AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        	  alarmManager.cancel(pendingIntent);
        	  



        	           // Tell the user about what we did.

        	           Toast.makeText(AlarmActivity.this, "Service stopped", Toast.LENGTH_LONG).show();





        	 }});
        
        
        
        Button contacts = (Button) findViewById(R.id.contacts);
        
        contacts.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
				startActivityForResult(intent, PICK_CONTACT);
				//onActivityResult(PICK_CONTACT,intent);
             }
            
            
            
			public void onActivityResult(int requestCode, int resultCode,Intent intent) 
            {
            	Log.d("contacts", "--------- >>>>>>  here 4");
            	
              if (requestCode == PICK_CONTACT)
              {   
            	  if(resultCode  == RESULT_OK)
            	  {
            		  Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
                      cursor.moveToNext();
                      String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                      String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
                      String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));                  
                     
                      
                      Cursor phones = getContentResolver().query( 
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                              null, null);
                    phones.moveToFirst();
                    number = phones.getString(phones.getColumnIndex("data1"));
                    Log.d("contacts", "--------- >>>>>>  here 232323");  
                    
                    phones.close();
                      
                      
    				Toast.makeText(AlarmActivity.this, "Contact LIST  =  "+ number, Toast.LENGTH_LONG).show(); 
                      Pnumber.setText(name);
                      Log.d("contacts name ", name );
                      Log.d("contacts ID ", contactId );
                      Log.d("contacts number ", number );
            		  
                      startActivity(new Intent(Intent.ACTION_VIEW));
                      
            		  
            	  }
                  
              }
            }
            
            
            
            
            
                        
            
            
         });

        
        
       
        
        
        
        
        
        
        
        
        
        
    }
}